package com.hlf.modules.merchant.goods.service.impl;

import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.Query;
import com.hlf.modules.api.entity.Print;
import com.hlf.modules.api.mapper.PrintMapper;
import com.hlf.modules.merchant.goods.entity.GoodsType;
import com.hlf.modules.merchant.goods.mapper.GoodsMapper;
import com.hlf.modules.merchant.goods.mapper.GoodsTypeMapper;
import com.hlf.modules.merchant.goods.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("goodsTypeService")
public class GoodsTypeServiceImpl implements GoodsTypeService {
	@Autowired
	private GoodsTypeMapper goodsTypeMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private PrintMapper printMapper;

	@Override
	public GoodsType queryObject(Long id) {
		return goodsTypeMapper.queryObject(id);
	}
	@Override
	public int queryTotal(Map<String, Object> map) {
		return goodsTypeMapper.queryTotal(map);
	}
	@Override
	public void save(GoodsType goodsType) {
		goodsTypeMapper.save(goodsType);
	}

	@Override
	public List<GoodsType> batchQueryListStoreId(Long storeId) {
		return goodsTypeMapper.batchQueryListStoreId(storeId);
	}

	/**
	 * 根据主键选择性修改
	 * @param goodsType
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(GoodsType goodsType) {
		return goodsTypeMapper.updateByPrimaryKeySelective(goodsType);
	}

	@Override
	public void delete(Long id) {
		goodsTypeMapper.delete(id);
	}
	@Override
	public void deleteBatch(Long[] ids) {
		goodsTypeMapper.deleteBatch(ids);
	}
	/**
	 * 分页查询
	 */
	@Override
	public PageUtils queryListForPage(Map<String, Object> map) {
		Query query = new Query(map);
		int total = goodsTypeMapper.queryTotal(query);
		List<GoodsType> goodsTypeList=null;
		if(total>0) {
			goodsTypeList=  goodsTypeMapper.queryList(query);
			for (GoodsType goodsType : goodsTypeList) {
				Integer num = goodsMapper.selectCountByGoodsTypeId(goodsType.getId());
				goodsType.setGoodsNum(num);
				Long id = goodsType.getPrinterId();
				if (id == null){
					continue;
				}
				Print print = printMapper.selectByPrimaryKey(id);
				if (print == null){
					continue;
				}
				goodsType.setPrintName(print.getName());
			}
		}
		return new PageUtils(goodsTypeList, total, query.getLimit(), query.getPage());
	}

	/**
	 * 根据商铺id查询,商品类型集合
	 * @param storeId
	 * @return
	 */
	@Override
	public List<GoodsType> queryListForStoreId(String storeId) {
		return goodsTypeMapper.queryListForStoreId(storeId);
	}

	/**
	 * 根据多个ids查询
	 * @param goodsTypeIdList
	 * @return
	 */
	@Override
	public List<GoodsType> queryListForIds(List<Long> goodsTypeIdList) {
		return goodsTypeMapper.queryListForIds(goodsTypeIdList);
	}

	/**
	 * 批量逻辑删除
	 * @param goodsTypeIdList
	 * @return
	 */
	@Override
	public int batchUpdateDel(List<Long> goodsTypeIdList, Long userId){
		for (Long aLong : goodsTypeIdList) {
			int i = goodsMapper.selectCountByGoodsTypeId(aLong);
			if (i > 0){
				throw new BusinessException("该商品类型下还有商品,请解除关联后删除!");
			}
		}
		return goodsTypeMapper.batchUpdateDel(goodsTypeIdList,userId);
	}

	/**
	 * 选择性新增
	 * @param goodsType
	 * @return
	 */
	@Override
	public int insertSelective(GoodsType goodsType) {
		return goodsTypeMapper.insertSelective(goodsType);
	}


}
